// src/router/index.js
import Vue from 'vue';
import VueRouter from 'vue-router';
import HomeView from '../views/HomeView.vue';
import Login from '../views/Login.vue';
import ActivityManagement from '../views/ActivityManagement.vue'; // 添加缺失的视图组件

Vue.use(VueRouter);

const routes = [
  {
    path: '/login',
    name: 'login',
    component: Login
  },
  {
    path: '/',
    name: 'home',
    component: HomeView
  },
  {
    path: '/activity-management', // 添加新路由
    name: 'activity-management',
    component: ActivityManagement
  }
];

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
});

// src/router/index.js
router.beforeEach((to, from, next) => {
  const isAuthenticated = localStorage.getItem('user');
  const authRequired = !['/login'].includes(to.path);

  if (authRequired && !isAuthenticated) {
    next('/login');
  } else {
    next();
  }
});

export default router;